//展开
function _expand(treegridId){
    var node = j('#'+treegridId).treegrid('getSelected');
    if (node) {
        j('#'+treegridId).treegrid('expandAll', node.id);
    } else {
        j('#'+treegridId).treegrid('expandAll');
    }
}

//折叠
function _collapse(treegridId){
    var node = j('#'+treegridId).treegrid('getSelected');
    if (node) {
        j('#'+treegridId).treegrid('collapseAll', node.id);
    } else {
        j('#'+treegridId).treegrid('collapseAll');
    }
}

//tree刷新
function _treeRefresh(treegridId){
    j('#'+treegridId).treegrid('reload');
}

//datagrid刷新
function _datagridRefresh(datagridId){
    j('#'+datagridId).datagrid('reload');
}

//新增
function _addPage(panelId,title,url,datagridIdOrTreegridId){
    self.top.iframeDialog({
        id:panelId,
        width:700,
        height:600,
        title:"新增·"+title,
        src:url,
        onBeforeClose:function(){//对话框关闭前刷新当前页
            try{
                _datagridRefresh(datagridIdOrTreegridId);
            }catch(e) {
                _treeRefresh(datagridIdOrTreegridId);
            }
            return true;
        }
    });
}


//编辑
function _editPage(panelId,title,url,datagridIdOrTreegridId){
    self.top.iframeDialog({
        id:panelId,
        width:700,
        height:600,
        title:"修改·"+title,
        src:url,
        onBeforeClose:function(){//对话框关闭前刷新当前页
            try{
                _datagridRefresh(datagridIdOrTreegridId);
            }catch(e) {
                _treeRefresh(datagridIdOrTreegridId);
            }
            return true;
        }
    });
}


function _delete(url,datagridIdOrTreegridId){

    j.messager.confirm('警告','确认删除该记录吗？',function(r){
        if(r){

            j.messager.progress({
                title:'温馨提示',
                msg:'正在处理中...'
            });

            j.ajax({
                type:'POST',
                contentType: 'application/x-www-form-urlencoded; charset=utf-8', //默认就是如此 ，根本该不了，改也不起作用
                dataType:'json',
                url: url,
                success:function(data,textStatus){
                    try{
                        _datagridRefresh(datagridIdOrTreegridId);
                    }catch(e) {
                        _treeRefresh(datagridIdOrTreegridId);
                    }
                    j.messager.progress('close');
                    j.messager.show({
                        title: '温馨提示',
                        msg: data.message,
                        showType: 'show'
                    });
                },
                error:function(XMLHttpRequest,textStatus,errorThrown) {
                    j.messager.progress('close');
                    j.message.show({
                        title:'温馨提示',
                        msg:'删除失败！'
                    });
                }
            });
        }
    });
}

//ifram对话框
function iframeDialog(option){

    if(option.id==undefined){
        option.id="d"+(Math.round(Math.random()*(9999-1000)+1000));
        //alert("请为构建的对话框设定一个id!");
        //return;
    }
    if(option.title==undefined){
        option.title="对话框";
    }
    if(option.width==undefined){
        option.width=700;
    }
    if(option.height==undefined){
        option.height=600;
    }
    if(option.src==undefined){
        option.src="";
    }
    if(option.onBeforeClose==undefined){
        option.onBeforeClose=function(){return true;};
    }

    var window_height=j(window).height();
    var height_sub=window_height-option.height;
    if(height_sub<window_height/7)option.height=window_height-window_height/7;
    //if(option.height<500)option.height=500;

    var window_width=j(window).width();
    var width_sub=window_width-option.width;
    if(width_sub<window_width/7)option.width=window_width-window_width/7;

    option.closed=true;
    option.cache=false;
    option.modal=true;
    option.draggable=false;
    option.resizable=true;

    //1、创建dom
    if(j("#"+option.id).length>0){//页面已经加入dom，则删除dom
        j("#"+option.id).dialog('destroy',true);
    }
    var iframe=document.createElement("iframe");
    iframe.setAttribute("id",option.id+"_ifr");
    iframe.setAttribute("frameborder","0");
    iframe.setAttribute("scrolling","auto");
    iframe.setAttribute("width","100%");
    iframe.setAttribute("height","100%");

    var div=document.createElement("div");
    div.setAttribute("id",option.id);
    div.setAttribute("width",option.width);//div宽度
    div.setAttribute("height",option.height);//div高度
    div.appendChild(iframe);

    document.body.appendChild(div);


    //3、初始化对话框
    j("#"+option.id).dialog(option);


    //2、设置src
    j("#"+option.id+"_ifr").attr("src",option.src);

    //2、打开对话框
    j("#"+option.id).dialog('open');
}